<?php

class ChristmasService extends AbstractService
{
    const ActivityChristmas          = 30;  //翻牌子活动
    /**
     * @var ChristmasService
     */
    private static $instance;

    /**
     * Get instance of ChristmasService.
     *
     * @return ChristmasService
     */
    public static function get()
    {
        if (!self::$instance) {
            self::$instance = new ChristmasService();
        }
        return self::$instance;
    }

    /**
     * Christmas.
     */
    protected function __construct()
    {
        parent::__construct();
    }

    /**
     * 处理创建时的逻辑
     *
     * @param int $shardId
     * @return void
     */
    public function handleCreate($shardId)
    {
    }

    /**
     * 用户登录后触发逻辑
     *
     * @param int $userId
     * @return void
     */
    public function handleLogin($userId)
    {
    }

    /**
     * 提供当前 service 数据
     *
     * @param int $userId
     * @param boolean $visitMode default false
     * @return array(DebrisVoList, CollectionsVoList)
     */
    public function info($userId, $visitMode = false)
    {
    }

    /**
     * 购买好友赠送的礼物
     * @param $userId
     * @param $aid
     * @param $friendUid
     * @param $number
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function useCashFriendGift($userId, $aid, $friendUid, $number, $reqTime)
    {
        $res = array(
            'code' => 0,
            'friendGift'=> array()
        );
        $activity = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GAME_EVENT, $aid, true);
        if (!$activity || empty($activity)){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['CONFIG_NOT_EXIST'];//配置不存在
            return $res;
        }

        //获取玩家数据
        /**
         * @var $christmasCommonList ChristmasCommonVoList
         * @var $christmasFloorData ChristmasCommonVo
         */
        $christmasCommonList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_ACTIVITY_CHRISTMAS_COMMON);
        $christmasFloorData = $christmasCommonList->getElement($aid,false);
        if (!$christmasFloorData) {
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['DATA_NOT_EXIST'];//数据不存在
            return $res;
        }

        //获取玩家自己的活动时间
        $platform = UserService::get()->getInfo($userId, GameConst::USER_INFO_TYPE_USER_PLATFORM);
        $platformPre = GameConst::PLATFORM_TIME_ARRAY[$platform];
        $startTime = strtotime($activity[$platformPre.'TriggerTime']);
        $endTime = strtotime($activity[$platformPre.'EndTime']);
        if ($christmasFloorData){
            $startTime = $christmasFloorData->getCreateTime();
            $endTime = $christmasFloorData->getEndTime();
        }
        if (!$startTime || $startTime > $reqTime) {
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['START_ERR'];//开始时间错误
            return $res;
        }
        if (!$endTime || $endTime <= $reqTime) {
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['END_ERR'];//结束时间错误
            return $res;
        }
        if ($activity['type'] != self::ActivityChristmas) {
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['ACT_TYPE_ERR'];//活动类型错误
            return $res;
        }

        //获取玩家今日领取礼物的数据
        /**
         * @var $christmasSendList ChristmasSendGiftVoList
         * @var $christmasSendData ChristmasSendGiftVo
         */
        $christmasSendList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_ACTIVITY_CHRISTMAS_SEND_GIFT);
        $christmasSendData = $christmasSendList->getElement($aid,false);
        if (!$christmasSendData){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['DATA_NOT_EXIST'];//数据不存在
            return $res;
        }

        $commonRedisDb = SzDbFactory::get()->getDb(null, SzAbstractDb::DB_TYPE_REDIS);
        //获取玩家送礼配对标记
        $direction = '';
        $flag1 = $commonRedisDb->get(GameConst::CACHE_KEY_CHRISTMAS_UT_FLAG.$userId.'-'.$friendUid.'-'.$aid);
        $flag2 = $commonRedisDb->get(GameConst::CACHE_KEY_CHRISTMAS_UT_FLAG.$friendUid.'-'.$userId.'-'.$aid);
        if (!$flag1 && !$flag2){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['DATA_NOT_EXIST'];//数据不存在
            return $res;
        }
        else{
            //设置获取方向
            if ($flag1){
                $direction = $userId.'-'.$friendUid;
            }
            if ($flag2){
                $direction = $friendUid.'-'.$userId;
            }
        }
        if ($direction == ''){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['DATA_NOT_EXIST'];//数据不存在
            return $res;
        }

        //获取所有好友赠送的礼物
        $list = $commonRedisDb->lRange(GameConst::CACHE_KEY_CHRISTMAS_GET_GIFT_LIST.$direction.'-'.$aid,0,-1);
        if (!$list){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['DATA_NOT_EXIST'];//数据不存在
            return $res;
        }

        $friendSendData = array();
        $tempId = -1;
        $mineBuy = 0;
        $backBuy = 0;
        if ($list) {
            foreach ($list as $key => $val) {
                $val = json_decode($val,true);
                if ($val['id'] == $number && ($val['sendUid'] == $friendUid || $val['getUid'] == $friendUid)){
                    $friendSendData = $val;
                    $tempId = $key;
                    if ($val['sendUid'] == $friendUid){
                        //买别人送的礼物
                        $mineBuy = 1;
                    }
                    if ($val['getUid'] == $friendUid){
                        //买别人回的礼物
                        $backBuy = 1;
                    }
                }
            }
        }

        if (empty($friendSendData) || $tempId < 0){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['DATA_NOT_EXIST'];//数据不存在
            return $res;
        }
        if ($friendSendData['isGet'] && $friendSendData['isGetBack']){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['GIFT_HAS_GET'];//礼物已经领取
            return $res;
        }
        if ($mineBuy){
            if ($friendSendData['isGet']){
                $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['GIFT_HAS_GET'];//礼物已经领取
                return $res;
            }
        }
        if ($backBuy){
            if ($friendSendData['isGetBack']){
                $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['GIFT_HAS_GET'];//礼物已经领取
                return $res;
            }
        }

        //消耗钻石
        $buyNum = SzConfig::get()->loadAppConfig(ModuleProfileConst::CONFIG_PROFILE, 'treasureReceiveBuy', true)['value'];
        GameService::get()->handleEntity($userId, GameConst::ENTITY_ACTION_CONSUME_MONEY, $buyNum,  true);
        CommonFunctionService::get()->reportResourceUseDetails(GameConst::FUNC_CHRISTMAS_FUNC,$userId,0,GameConst::ENTITY_ACTION_CONSUME_MONEY.':'.$buyNum.'|','buyFriendGift');//获得资源上报

        $giftType = 0;
        //玩家获得礼物
        if ($mineBuy){
            $giftType = $friendSendData['sendType'];
        }
        if ($backBuy){
            $giftType = $friendSendData['backType'];
        }

        $multiple = 1;
        $getGift = json_decode(SzConfig::get()->loadAppConfig(ModuleProfileConst::CONFIG_PROFILE, 'treasureGiftReward', true)['value']);
        if ($giftType){
            $multiple = 2;
        }
        $input = '';
        foreach ($getGift as $itemId=>$num){
            if ($itemId == GameConst::ENTITY_ACTION_GET_EXP) {
                UserService::get()->updateExp($userId, $num*$multiple,null, false);
            } else {
                GameService::get()->handleEntity($userId, $itemId, $num*$multiple,  true);
                $input .= $itemId.':'.$num*$multiple.'|';
            }
        }
        CommonFunctionService::get()->reportResourceUseDetails(GameConst::FUNC_CHRISTMAS_FUNC,$userId,1,$input,'buyFriendGift');//获得资源上报

        //更新领奖次数
        $christmasSendData->setGetReward($christmasSendData->getGetReward() + 1);
        $christmasSendData->setUpdateTime($reqTime);
        $christmasSendList->updateElement($aid, $christmasSendData);
        SzPersister::get()->setVoList($christmasSendList);

        //更新礼物状态
        if ($friendSendData['isGet'] && $friendSendData['isGetBack']){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['GIFT_HAS_GET'];//礼物已经领取
            return $res;
        }

        if ($mineBuy){
            $friendSendData['isGet'] = 1;
        }
        if ($backBuy){
            $friendSendData['isGetBack'] = 1;
        }
        $commonRedisDb->lSet(GameConst::CACHE_KEY_CHRISTMAS_GET_GIFT_LIST.$direction.'-'.$aid,$tempId,json_encode($friendSendData));

        $res['friendGift'] = $friendSendData;
        return $res;
    }

    /**
     * 获得好友赠送的礼物
     * @param $userId
     * @param $aid
     * @param $friendUid
     * @param $number
     * @param $isBack
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function getFriendGift($userId, $aid, $friendUid, $number, $isBack, $reqTime)
    {
        $res = array(
            'code' => 0,
            'friendGift'=> array()
        );
        $activity = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GAME_EVENT, $aid, true);
        if (!$activity || empty($activity)){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['CONFIG_NOT_EXIST'];//配置不存在
            return $res;
        }

        //获取玩家数据
        /**
         * @var $christmasCommonList ChristmasCommonVoList
         * @var $christmasFloorData ChristmasCommonVo
         */
        $christmasCommonList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_ACTIVITY_CHRISTMAS_COMMON);
        $christmasFloorData = $christmasCommonList->getElement($aid,false);
        if (!$christmasFloorData) {
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['DATA_NOT_EXIST'];//数据不存在
            return $res;
        }

        //获取玩家自己的活动时间
        $platform = UserService::get()->getInfo($userId, GameConst::USER_INFO_TYPE_USER_PLATFORM);
        $platformPre = GameConst::PLATFORM_TIME_ARRAY[$platform];
        $startTime = strtotime($activity[$platformPre.'TriggerTime']);
        $endTime = strtotime($activity[$platformPre.'EndTime']);
        if ($christmasFloorData){
            $startTime = $christmasFloorData->getCreateTime();
            $endTime = $christmasFloorData->getEndTime();
        }
        if (!$startTime || $startTime > $reqTime) {
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['START_ERR'];//开始时间错误
            return $res;
        }
        if (!$endTime || $endTime <= $reqTime) {
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['END_ERR'];//结束时间错误
            return $res;
        }
        if ($activity['type'] != self::ActivityChristmas) {
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['ACT_TYPE_ERR'];//活动类型错误
            return $res;
        }

        //获取玩家今日领取礼物的数据
        /**
         * @var $christmasSendList ChristmasSendGiftVoList
         * @var $christmasSendData ChristmasSendGiftVo
         */
        $christmasSendList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_ACTIVITY_CHRISTMAS_SEND_GIFT);
        $christmasSendData = $christmasSendList->getElement($aid,false);
        if (!$christmasSendData){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['DATA_NOT_EXIST'];//数据不存在
            return $res;
        }
        $todayGetGiftNum = $christmasSendData->getGetReward();
        $dayLimit = SzConfig::get()->loadAppConfig(ModuleProfileConst::CONFIG_PROFILE, 'treasureGiftReceive', true)['value'];;
        if ($todayGetGiftNum >= $dayLimit){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['TIMES_MAX_LIMIT'];//领取次数已达上限
            return $res;
        }

        $commonRedisDb = SzDbFactory::get()->getDb(null, SzAbstractDb::DB_TYPE_REDIS);
        //获取玩家送礼配对标记
        $direction = '';
        $flag1 = $commonRedisDb->get(GameConst::CACHE_KEY_CHRISTMAS_UT_FLAG.$userId.'-'.$friendUid.'-'.$aid);
        $flag2 = $commonRedisDb->get(GameConst::CACHE_KEY_CHRISTMAS_UT_FLAG.$friendUid.'-'.$userId.'-'.$aid);
        if (!$flag1 && !$flag2){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['DATA_NOT_EXIST'];//数据不存在
            return $res;
        }
        else{
            //设置获取方向
            if ($flag1){
                $direction = $userId.'-'.$friendUid;
            }
            if ($flag2){
                $direction = $friendUid.'-'.$userId;
            }
        }
        if ($direction == ''){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['DATA_NOT_EXIST'];//数据不存在
            return $res;
        }

        //获取所有好友赠送的礼物
        $list = $commonRedisDb->lRange(GameConst::CACHE_KEY_CHRISTMAS_GET_GIFT_LIST.$direction.'-'.$aid,0,-1);
        if (!$list){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['DATA_NOT_EXIST'];//数据不存在
            return $res;
        }

        $friendSendData = array();
        $tempId = -1;
        if ($list) {
            foreach ($list as $key => $val) {
                $val = json_decode($val,true);
                if ($val['id'] == $number){
                    $friendSendData = $val;
                    $tempId = $key;
                }
            }
        }

        if (empty($friendSendData) || $tempId < 0){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['DATA_NOT_EXIST'];//数据不存在
            return $res;
        }

        $canGetType = -1;
        if ($isBack){
            if (!$friendSendData['isBack']){
                $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['GIFT_NO_BACK'];//没有回礼
                return $res;
            }
            $canGetType = $friendSendData['backType'];
            $friendSendData['isGetBack'] = 1;
        }
        else{
            if ($friendSendData['isGet']){
                $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['GIFT_HAS_GET'];//礼物已经领取
                return $res;
            }
            $canGetType = $friendSendData['sendType'];
            $friendSendData['isGet'] = 1;
        }

        //玩家获得礼物
        $multiple = 1;
        $getGift = json_decode(SzConfig::get()->loadAppConfig(ModuleProfileConst::CONFIG_PROFILE, 'treasureGiftReward', true)['value']);
        if ($canGetType < 0){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['DATA_NOT_EXIST'];//数据不存在
            return $res;
        }
        if ($canGetType){
            $multiple = 2;
        }

        $input = '';
        foreach ($getGift as $itemId=>$num){
            if ($itemId == GameConst::ENTITY_ACTION_GET_EXP) {
                UserService::get()->updateExp($userId, $num*$multiple,null, false);
                //跟新店铺经营数据（经验）
                UserShopService::get()->record($userId,2,14000001,$num*$multiple);
            } else {
                GameService::get()->handleEntity($userId, $itemId, $num*$multiple,  true);
                $input .= $itemId.':'.$num*$multiple.'|';
            }
        }
        CommonFunctionService::get()->reportResourceUseDetails(GameConst::FUNC_CHRISTMAS_FUNC,$userId,1,$input,'getFriendGift');//获得资源上报

        //更新领奖次数
        $christmasSendData->setGetReward($todayGetGiftNum + 1);
        $christmasSendData->setUpdateTime($reqTime);
        $christmasSendList->updateElement($aid, $christmasSendData);
        SzPersister::get()->setVoList($christmasSendList);

        //更新礼物状态
        $commonRedisDb->lSet(GameConst::CACHE_KEY_CHRISTMAS_GET_GIFT_LIST.$direction.'-'.$aid,$tempId,json_encode($friendSendData));

        $res['friendGift'] = $friendSendData;
        return $res;
    }

    /**
     * 商店概况
     * @param $userId
     * @param $aid
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function shopInfo($userId, $aid, $reqTime)
    {
        $res = array(
            'code' => 0,
            'socks'=> 0,
            'reward'=> '',
            'shop'=> array()
        );
        $activity = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GAME_EVENT, $aid, true);
        if (!$activity || empty($activity)){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['CONFIG_NOT_EXIST'];//配置不存在
            return $res;
        }

        //获取玩家数据
        /**
         * @var $christmasCommonList ChristmasCommonVoList
         * @var $christmasFloorData ChristmasCommonVo
         */
        $christmasCommonList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_ACTIVITY_CHRISTMAS_COMMON);
        $christmasFloorData = $christmasCommonList->getElement($aid,false);
        if (!$christmasFloorData) {
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['DATA_NOT_EXIST'];//数据不存在
            return $res;
        }

        //获取玩家自己的活动时间
        $platform = UserService::get()->getInfo($userId, GameConst::USER_INFO_TYPE_USER_PLATFORM);
        $platformPre = GameConst::PLATFORM_TIME_ARRAY[$platform];
        $startTime = strtotime($activity[$platformPre.'TriggerTime']);
        $endTime = strtotime($activity[$platformPre.'EndTime']);
        if ($christmasFloorData){
            $startTime = $christmasFloorData->getCreateTime();
            $endTime = $christmasFloorData->getEndTime();
        }
        if (!$startTime || $startTime > $reqTime) {
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['START_ERR'];//开始时间错误
            return $res;
        }
        if (!$endTime || $endTime <= $reqTime) {
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['END_ERR'];//结束时间错误
            return $res;
        }
        if ($activity['type'] != self::ActivityChristmas) {
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['ACT_TYPE_ERR'];//活动类型错误
            return $res;
        }

        $res['socks'] = $christmasFloorData->getRocksNum();
        $res['reward'] = json_decode($christmasFloorData->getRocksReward());
        //获取玩家商店购买数据
        /**
         * @var $christmasShopList ChristmasShopVoList
         * @var $christmasShopData ChristmasShopVo
         */
        $shopData = array();
        $christmasShopList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_ACTIVITY_CHRISTMAS_SHOP);
        foreach ($christmasShopList->toArray() as $pos=>$data){
            $christmasShopData = $christmasShopList->getElement($pos,false);
            if ($christmasShopData) {
                //校验是否隔天
                $reqDay = (int)date("Ymd", $reqTime);
                $updateDay = (int)date("Ymd", $christmasShopData->getUpdateTime());
                if ($reqDay > $updateDay){
                    $christmasShopData->setBuyTimes(0);
                    $christmasShopData->setUpdateTime($reqTime);
                    $christmasShopList->updateElement($pos, $christmasShopData);
                }
            }
            $shopData[] = $this->formatShopData($christmasShopData->toArray());
        }
        SzPersister::get()->setVoList($christmasShopList);

        $res['shop'] = $shopData;
        return $res;
    }

    /**
     * 每日限购
     * @param $userId
     * @param $aid
     * @param $pos
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function buyItem($userId, $aid, $pos, $reqTime)
    {
        $res = array(
            'code' => 0,
            'state'=> false
        );
        $activity = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GAME_EVENT, $aid, true);
        $storeConf = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GAME_ACTIVITY_CHRISTMAS_STORE, $pos, true);
        if (!$activity || empty($activity) || !$storeConf || empty($storeConf)){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['CONFIG_NOT_EXIST'];//配置不存在
            return $res;
        }

        //获取玩家数据
        /**
         * @var $christmasCommonList ChristmasCommonVoList
         * @var $christmasFloorData ChristmasCommonVo
         */
        $christmasCommonList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_ACTIVITY_CHRISTMAS_COMMON);
        $christmasFloorData = $christmasCommonList->getElement($aid,false);
        if (!$christmasFloorData) {
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['DATA_NOT_EXIST'];//数据不存在
            return $res;
        }

        $platform = UserService::get()->getInfo($userId, GameConst::USER_INFO_TYPE_USER_PLATFORM);
        $platformPre = GameConst::PLATFORM_TIME_ARRAY[$platform];
        $startTime = strtotime($activity[$platformPre.'TriggerTime']);
        $endTime = strtotime($activity[$platformPre.'EndTime']);
        if ($christmasFloorData){
            $startTime = $christmasFloorData->getCreateTime();
            $endTime = $christmasFloorData->getEndTime();
        }
        if (!$startTime || $startTime > $reqTime) {
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['START_ERR'];//开始时间错误
            return $res;
        }
        if (!$endTime || $endTime <= $reqTime) {
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['END_ERR'];//结束时间错误
            return $res;
        }
        if ($activity['type'] != self::ActivityChristmas) {
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['ACT_TYPE_ERR'];//活动类型错误
            return $res;
        }

        //获取玩家商店购买数据
        /**
         * @var $christmasShopList ChristmasShopVoList
         * @var $christmasShopData ChristmasShopVo
         */
        $christmasShopList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_ACTIVITY_CHRISTMAS_SHOP);
        $christmasShopData = $christmasShopList->getElement($pos,false);
        $useItemNum = 0;
        if (!$christmasShopData){
            $christmasShopData = new ChristmasShopVo(
                $userId,
                $pos,
                1,
                $reqTime,
                $reqTime,
                $endTime,
                true
            );
            $christmasShopList->addElement($christmasShopData);
            $christmasShopList->setElement($pos,$christmasShopData);
            $useItemNum = $storeConf['price'];
        }
        else{
            $hasBuy = $christmasShopData->getBuyTimes();
            $useItemNum = $storeConf['price'];
            if ($pos == 3){
                if ($storeConf['priceType']){
                    $useItemNum = $storeConf['price']*($hasBuy + 1);
                }
                if ($hasBuy >= 3){
                    $useItemNum = 150;
                }
            }
            else{
                if ($hasBuy >= $storeConf['buyLimit']){
                    $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['OUT_LIMIT'];//超过购买限制
                    return $res;
                }
            }
            $christmasShopData->setUpdateTime($reqTime);
            $christmasShopData->setBuyTimes($hasBuy + 1);
            $christmasShopList->updateElement($pos, $christmasShopData);
        }
        SzPersister::get()->setVoList($christmasShopList);

        //进行消耗
        GameService::get()->handleEntity($userId,GameConst::ENTITY_ACTION_CONSUME_MONEY, $useItemNum,  true);
        CommonFunctionService::get()->reportResourceUseDetails(GameConst::FUNC_CHRISTMAS_FUNC,$userId,0,GameConst::ENTITY_ACTION_CONSUME_MONEY.':'.$useItemNum.'|','buyItem');//消耗资源上报


        if ($pos == 3){
            $christmasFloorData->setPhysical($christmasFloorData->getPhysical() + $storeConf['itemId'][720141013027]);
            $christmasCommonList->updateElement($aid, $christmasFloorData);
        }
        else{
            //获得道具
            foreach ($storeConf['itemId'] as $itemId=>$num){
                GameService::get()->handleEntity($userId, $itemId, $num,  true);
                CommonFunctionService::get()->reportResourceUseDetails(GameConst::FUNC_CHRISTMAS_FUNC,$userId,1,$itemId.':'.$num.'|','buyItem');//获得资源上报
            }
        }
        SzPersister::get()->setVoList($christmasCommonList);

        $res['state'] = true;

        //限购埋点
        $userLevel = UserService::get()->getUserLevel($userId);
        StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_USER_ORBIT, array(
            'clientData' => array(
                'tid' => 'Module:newChristmas|action:buyItem|',
                'item' => $pos,
                'value' => 1,
                'value2' => $userLevel,
            )
        ));

        return $res;
    }

    /**
     * 获取被送礼数据
     * @param $userId
     * @param $aid
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function getGiftsData($userId, $aid, $reqTime)
    {
        $res = array(
            'code' => 0,
            'get' => 0,
            'friends'=> array()
        );
        $activity = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GAME_EVENT, $aid, true);
        if (!$activity || empty($activity)){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['CONFIG_NOT_EXIST'];//配置不存在
            return $res;
        }

        //获取玩家数据
        /**
         * @var $christmasCommonList ChristmasCommonVoList
         * @var $christmasFloorData ChristmasCommonVo
         */
        $christmasCommonList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_ACTIVITY_CHRISTMAS_COMMON);
        $christmasFloorData = $christmasCommonList->getElement($aid,false);
        if (!$christmasFloorData) {
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['DATA_NOT_EXIST'];//数据不存在
            return $res;
        }

        //获取玩家自己的活动时间
        $platform = UserService::get()->getInfo($userId, GameConst::USER_INFO_TYPE_USER_PLATFORM);
        $platformPre = GameConst::PLATFORM_TIME_ARRAY[$platform];
        $startTime = strtotime($activity[$platformPre.'TriggerTime']);
        $endTime = strtotime($activity[$platformPre.'EndTime']);
        if ($christmasFloorData){
            $startTime = $christmasFloorData->getCreateTime();
            $endTime = $christmasFloorData->getEndTime();
        }
        if (!$startTime || $startTime > $reqTime) {
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['START_ERR'];//开始时间错误
            return $res;
        }
        if (!$endTime || $endTime <= $reqTime) {
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['END_ERR'];//结束时间错误
            return $res;
        }
        if ($activity['type'] != self::ActivityChristmas) {
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['ACT_TYPE_ERR'];//活动类型错误
            return $res;
        }

        //获取玩家今日赠送礼物的数据
        /**
         * @var $christmasSendList ChristmasSendGiftVoList
         * @var $christmasSendData ChristmasSendGiftVo
         */
        $christmasSendList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_ACTIVITY_CHRISTMAS_SEND_GIFT);
        $christmasSendData = $christmasSendList->getElement($aid,false);
        if (!$christmasSendData){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['DATA_NOT_EXIST'];//数据不存在
            return $res;
        }

        $totalGift = array();
        $commonRedisDb = SzDbFactory::get()->getDb(null, SzAbstractDb::DB_TYPE_REDIS);
        $direction = '';
        //设置好友收礼名单
        $friendRedisDb = SzDbFactory::get()->getDb($userId, SzAbstractDb::DB_TYPE_REDIS);
        //设置回礼数据
        $giftCell = $friendRedisDb->get(GameConst::CACHE_KEY_CHRISTMAS_GIFT_CELL.$userId.'-'.$aid);
        $i = 0;
        if ($giftCell == '' || $giftCell == null || $giftCell == 0){
            return $res;
        }
        else{
            $sendUids = explode(',',$giftCell);
            foreach ($sendUids as $key=>$sendUid){
                //获取玩家送礼配对标记
                $flag1 = $commonRedisDb->get(GameConst::CACHE_KEY_CHRISTMAS_UT_FLAG.$userId.'-'.$sendUid.'-'.$aid);
                $flag2 = $commonRedisDb->get(GameConst::CACHE_KEY_CHRISTMAS_UT_FLAG.$sendUid.'-'.$userId.'-'.$aid);
                if (!$flag1 && !$flag2){
                    continue;
                }
                else{
                    //设置获取方向
                    if ($flag1){
                        $direction = $userId.'-'.$sendUid;
                    }
                    if ($flag2){
                        $direction = $sendUid.'-'.$userId;
                    }
                }
                if ($direction == ''){
                    continue;
                }

                $list = $commonRedisDb->lRange(GameConst::CACHE_KEY_CHRISTMAS_GET_GIFT_LIST.$direction.'-'.$aid,0,-1);
                if ($list) {
                    foreach ($list as $key => $val) {
                        $val = json_decode($val,true);
                        if ($val['sendTime'] >= ($reqTime - GameConst::SOCIAL_VALID_TIME + 86400)) {
                            $totalGift[$i]['id'] = $val['id'];
                            $totalGift[$i]['sendUid'] = $val['sendUid'];
                            $totalGift[$i]['sendType'] = $val['sendType'];
                            $totalGift[$i]['sendTime'] = $val['sendTime'];
                            $totalGift[$i]['isGet'] = $val['isGet'];
                            $totalGift[$i]['isBack'] = $val['isBack'];
                            $totalGift[$i]['isGetBack'] = $val['isGetBack'];
                            $totalGift[$i]['backTime'] = $val['backTime'];
                            $totalGift[$i]['getUid'] = $val['getUid'];
                            $totalGift[$i]['backType'] = $val['backType'];
                            $i++;
                        }
                    }
                }
            }
        }

//        //获取玩家所有的被赠送数据
//        $redisDb = SzDbFactory::get()->getDb($userId, SzAbstractDb::DB_TYPE_REDIS);
//        $list = $redisDb->lRange(GameConst::CACHE_KEY_CHRISTMAS_GET_GIFT_LIST.$userId,0,-1);
//        $friendsData = array();
//        if ($list) {
//            foreach ($list as $key => $val) {
//                $val = json_decode($val,true);
//                if ($val['time'] < ($reqTime - GameConst::SOCIAL_VALID_TIME + 86400)) {
//                    continue;
//                }
//                $friendsData[$key]['id'] = $val['id'];
//                $friendsData[$key]['sendUid'] = $val['sendUid'];
//                $friendsData[$key]['giftType'] = $val['giftType'];
//                $friendsData[$key]['time'] = $val['time'];
//                $friendsData[$key]['isGet'] = $val['isGet'];
//                $friendsData[$key]['back'] = $val['back'];
//            }
//        }

        $res['get'] = $christmasSendData->getGetReward();
        $res['friends'] = $totalGift;
        return $res;
    }

    /**
     * 获取订单数据
     * @param $userId
     * @param $aid
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function getOrderData($userId, $aid, $reqTime)
    {
        $res = array(
            'code' => 0,
            'orderData'=> array()
        );
        $activity = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GAME_EVENT, $aid, true);
        if (!$activity || empty($activity)){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['CONFIG_NOT_EXIST'];//配置不存在
            return $res;
        }

        //获取玩家数据
        /**
         * @var $christmasCommonList ChristmasCommonVoList
         * @var $christmasFloorData ChristmasCommonVo
         */
        $christmasCommonList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_ACTIVITY_CHRISTMAS_COMMON);
        $christmasFloorData = $christmasCommonList->getElement($aid,false);
        if (!$christmasFloorData) {
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['DATA_NOT_EXIST'];//数据不存在
            return $res;
        }

        //获取玩家自己的活动时间
        $platform = UserService::get()->getInfo($userId, GameConst::USER_INFO_TYPE_USER_PLATFORM);
        $platformPre = GameConst::PLATFORM_TIME_ARRAY[$platform];
        $startTime = strtotime($activity[$platformPre.'TriggerTime']);
        $endTime = strtotime($activity[$platformPre.'EndTime']);
        if ($christmasFloorData){
            $startTime = $christmasFloorData->getCreateTime();
            $endTime = $christmasFloorData->getEndTime();
        }
        if (!$startTime || $startTime > $reqTime) {
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['START_ERR'];//开始时间错误
            return $res;
        }
        if (!$endTime || $endTime <= $reqTime) {
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['END_ERR'];//结束时间错误
            return $res;
        }
        if ($activity['type'] != self::ActivityChristmas) {
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['ACT_TYPE_ERR'];//活动类型错误
            return $res;
        }

        //获取玩家所有的订单格子数据
        $orders = array();
        /**
         * @var $christmasCellsList ChristmasCellVoList
         * @var $christmasCell ChristmasCellVo
         */
        $christmasCellsList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_ACTIVITY_CHRISTMAS_CELL);
        foreach ($christmasCellsList->toArray() as $key=>$data){
            if ($data['type'] == GameConst::CHRISTMAS_CELL_ORDER){
                $orders[] = array(
                    'cell' => $data['cell'],
                    'content' => $data['content'],
                    'reward' => $data['reward']
                );
            }
        }

        $res['orderData'] = $orders;
        return $res;
    }

    /**
     * 获取主动送礼数据
     * @param $userId
     * @param $aid
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function getSendGiftsData($userId, $aid, $reqTime)
    {
        $res = array(
            'code' => 0,
            'send' => 0,
            'friends'=> array()
        );
        $activity = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GAME_EVENT, $aid, true);
        if (!$activity || empty($activity)){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['CONFIG_NOT_EXIST'];//配置不存在
            return $res;
        }

        //获取玩家数据
        /**
         * @var $christmasCommonList ChristmasCommonVoList
         * @var $christmasFloorData ChristmasCommonVo
         */
        $christmasCommonList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_ACTIVITY_CHRISTMAS_COMMON);
        $christmasFloorData = $christmasCommonList->getElement($aid,false);
        if (!$christmasFloorData) {
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['DATA_NOT_EXIST'];//数据不存在
            return $res;
        }

        //获取玩家自己的活动时间
        $platform = UserService::get()->getInfo($userId, GameConst::USER_INFO_TYPE_USER_PLATFORM);
        $platformPre = GameConst::PLATFORM_TIME_ARRAY[$platform];
        $startTime = strtotime($activity[$platformPre.'TriggerTime']);
        $endTime = strtotime($activity[$platformPre.'EndTime']);
        if ($christmasFloorData){
            $startTime = $christmasFloorData->getCreateTime();
            $endTime = $christmasFloorData->getEndTime();
        }
        if (!$startTime || $startTime > $reqTime) {
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['START_ERR'];//开始时间错误
            return $res;
        }
        if (!$endTime || $endTime <= $reqTime) {
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['END_ERR'];//结束时间错误
            return $res;
        }
        if ($activity['type'] != self::ActivityChristmas) {
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['ACT_TYPE_ERR'];//活动类型错误
            return $res;
        }

        //获取玩家今日赠送礼物的数据
        /**
         * @var $christmasSendList ChristmasSendGiftVoList
         * @var $christmasSendData ChristmasSendGiftVo
         */
        $christmasSendList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_ACTIVITY_CHRISTMAS_SEND_GIFT);
        $christmasSendData = $christmasSendList->getElement($aid,false);
        if (!$christmasSendData){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['DATA_NOT_EXIST'];//数据不存在
            return $res;
        }

        //隔天刷新
        $lastTime = $christmasSendData->getUpdateTime();
        //校验是否隔天
        $reqDay = (int)date("Ymd", $reqTime);
        $updateDay = (int)date("Ymd",$lastTime);
        $isNextDay = false;

        if ($reqDay > $updateDay){
            if(json_decode($christmasSendData->getTargetUids() != '')){
                $christmasSendData->setTargetUids('');
            }
            $christmasSendData->setUpdateTime($reqTime);
            $christmasSendData->setGetReward(0);
            $christmasSendData->setSendTimes(0);

            $christmasSendList->updateElement($aid, $christmasSendData);
            SzPersister::get()->setVoList($christmasSendList);
            $isNextDay = true;
        }

        //获取玩家的所有好友
        $friends = FriendService::get()->getFriendInfoList($userId);
        if (empty($friends)){
            return $res;
        }

        $joinFriendArr = array();
        //过滤出参加活动的好友
        foreach ($friends as $fid=>$data){
            //获取好友数据
            /**
             * @var $christmasFriendCommonList ChristmasCommonVoList
             * @var $christmasFriendFloorData ChristmasCommonVo
             */
            $christmasFriendCommonList = SzPersister::get()->getVoList($fid, GameConst::ORM_NAME_ACTIVITY_CHRISTMAS_COMMON);
            $christmasFriendFloorData = $christmasFriendCommonList->getElement($aid,false);
            if ($christmasFriendFloorData){
                $joinFriendArr[$fid] = 0;
            }
        }

        if (empty($joinFriendArr)){
            return $res;
        }

        if ($isNextDay){
            $res['friends'] = $joinFriendArr;
            return $res;
        }

        $todaySendFriends = explode(',',json_decode($christmasSendData->getTargetUids()));
        foreach ($joinFriendArr as $fid=>$sendFlag){
            if (in_array($fid,$todaySendFriends)){
                $joinFriendArr[$fid] = 1;
            }
        }

        $res['send'] = $christmasSendData->getSendTimes();
        $res['friends'] = $joinFriendArr;
        return $res;
    }

    /**
     * 获取层级数据
     * @param $userId
     * @param $aid
     * @param $floor
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function floorInfo($userId, $aid, $floor, $reqTime)
    {
        $res = array(
            'code' => 0,
            'physical'=> 0,
            'bronze'=> 0,
            'silver'=> 0,
            'gold'=> 0,
            'floorData'=> array()
        );
        $activity = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GAME_EVENT, $aid, true);
        $cellsConf = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GAME_ACTIVITY_CHRISTMAS_CELL, $floor, true);
        $firstSend = SzConfig::get()->loadAppConfig(ModuleProfileConst::CONFIG_PROFILE, 'treasureEnergyFrist', true)['value'];
        $freeSend = SzConfig::get()->loadAppConfig(ModuleProfileConst::CONFIG_PROFILE, 'treasureEnergyFree', true)['value'];
        $specialCellConf = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GAME_ACTIVITY_CHRISTMAS_CELL_SPECIAL, null, true);

        $platform = UserService::get()->getInfo($userId, GameConst::USER_INFO_TYPE_USER_PLATFORM);
        $platformPre = GameConst::PLATFORM_TIME_ARRAY[$platform];
        $startTime = strtotime($activity[$platformPre.'TriggerTime']);
        $endTime = strtotime($activity[$platformPre.'EndTime']);
        $rangeTime = $activity['endTime'] - $activity['startTime'];
        if (!$activity || empty($activity) || !$firstSend || !$freeSend || !$cellsConf || empty($cellsConf) || !$specialCellConf || empty($specialCellConf)){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['CONFIG_NOT_EXIST'];//配置不存在
            return $res;
        }

        //获取玩家数据
        /**
         * @var $christmasCommonList ChristmasCommonVoList
         * @var $christmasFloorData ChristmasCommonVo
         */
        $christmasCommonList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_ACTIVITY_CHRISTMAS_COMMON);
        $christmasFloorData = $christmasCommonList->getElement($aid,false);
        $canSendPhysical = false;
        $days = 0;
        if ($christmasFloorData) {
            //获取玩家自己的活动时间
            $sendDayTime = strtotime(date("Y-m-d", $christmasFloorData->getSendPhysical()));
            $startTime = $christmasFloorData->getCreateTime();
            $endTime = $christmasFloorData->getEndTime();
            $days = floor(($reqTime - $sendDayTime)/86400);
            if ($days){
                $canSendPhysical = true;
            }
        }

        if ($startTime > $reqTime) {
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['START_ERR'];//开始时间错误
            return $res;
        }
        if ($endTime <= $reqTime) {
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['END_ERR'];//结束时间错误
            return $res;
        }
        if ($activity['type'] != self::ActivityChristmas) {
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['ACT_TYPE_ERR'];//活动类型错误
            return $res;
        }

        //是否可以赠送体力
        if (!$christmasFloorData) {
            $specialNum = 0;
            foreach ($specialCellConf as $key=>$data){
                if ($floor == $data['atFloor']){
                    $specialNum++;
                }
            }
            $specialStr = '';
            for ($i = 1; $i <= $specialNum; $i++ ){
                if ($specialStr == ''){
                    $specialStr = '0';
                }
                else{
                    $specialStr = $specialStr.',0';
                }
            }

            $christmasFloorData = new ChristmasCommonVo(
                $userId,
                $aid,
                $firstSend,
                0,
                1,
                0,
                0,
                0,
                0,
                0,
                0,
                0,
                0,
                json_encode($specialStr),
                json_encode(''),
                json_encode(''),
                0,
                json_encode('0,0,0'),
                $reqTime,
                $reqTime,
                $reqTime,
                $reqTime + $rangeTime,
                true
            );
            $christmasCommonList->addElement($christmasFloorData);
            $christmasCommonList->setElement($aid,$christmasFloorData);
        }
        else{
            if ($days && $canSendPhysical){
                $christmasFloorData->setPhysical($christmasFloorData->getPhysical() + $days*$freeSend);
                //更新送体力最新时间
                $christmasFloorData->setSendPhysical($reqTime);
                $christmasCommonList->updateElement($aid, $christmasFloorData);
            }
            $res['bronze'] = $christmasFloorData->getFirstTreasure();
            $res['silver'] = $christmasFloorData->getSecondTreasure();
            $res['gold'] = $christmasFloorData->getThirdTreasure();
        }
        SzPersister::get()->setVoList($christmasCommonList);

        //获取玩家当前层级的牌子数据
        /**
         * @var $christmasCellList ChristmasCellVoList
         */
        $cellDatas = array();
        $startCell = 0;
        $endCell = 0;
        $christmasCellList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_ACTIVITY_CHRISTMAS_CELL);
        if ($christmasCellList->getListCount() != 0){
            //整合牌子数据数据
            if ($floor == 1){
                $startCell = 1;
                $endCell = $cellsConf['squareNum'];
            }
            if ($floor == 2){
                $startCell = $cellsConf['squareNum'] - 100 + 1;
                $endCell = $startCell + $cellsConf['squareNum'] - 1;
            }
            if ($floor == 3){
                $startCell = $cellsConf['squareNum'] + 1;
                $endCell = $startCell + $cellsConf['squareNum'] - 1;
            }
            foreach ($christmasCellList->toArray() as $key=>$data){
                if ($data['cell'] >= $startCell && $data['cell'] <= $endCell){
                    $cellDatas[] = $this->formatCellData($data);
                }
                else{
                    continue;
                }
            }
        }

        //获取玩家今日赠送礼物数据
        /**
         * @var $christmasSendList ChristmasSendGiftVoList
         * @var $christmasSendData ChristmasSendGiftVo
         */
        $christmasSendList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_ACTIVITY_CHRISTMAS_SEND_GIFT);
        $christmasSendData = $christmasSendList->getElement($aid,false);
        if (!$christmasSendData){
            $christmasSendData = new ChristmasSendGiftVo(
                $userId,
                $aid,
                0,
                0,
                '',
                $reqTime,
                $reqTime,
                $endTime,
                true
            );
            $christmasSendList->addElement($christmasSendData);
            $christmasSendList->setElement($aid,$christmasSendData);
            SzPersister::get()->setVoList($christmasSendList);
        }

        $res['physical'] = $christmasFloorData->getPhysical() - $christmasFloorData->getUsePhysical();
        $res['floorData'] = $cellDatas;
        return $res;
    }

    /**
     * 翻牌子
     * @param $userId
     * @param $aid
     * @param $floor
     * @param $cell
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function turnCell($userId, $aid, $floor, $cell, $reqTime)
    {
        $res = array(
            'code' => 0,
            'num' => 0,
            'cell'=> array()
        );
        $activity = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GAME_EVENT, $aid, true);
        $cellsConf = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GAME_ACTIVITY_CHRISTMAS_CELL, $floor, true);
        $specialCellConf = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GAME_ACTIVITY_CHRISTMAS_CELL_SPECIAL, null, true);
        if (!$activity || empty($activity) || !$cellsConf || empty($cellsConf) || !$specialCellConf || empty($specialCellConf)){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['CONFIG_NOT_EXIST'];//配置不存在
            return $res;
        }

        //获取玩家数据
        /**
         * @var $christmasCommonList ChristmasCommonVoList
         * @var $christmasFloorData ChristmasCommonVo
         */
        $christmasCommonList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_ACTIVITY_CHRISTMAS_COMMON);
        $christmasFloorData = $christmasCommonList->getElement($aid,false);
        if (!$christmasFloorData) {
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['DATA_NOT_EXIST'];//数据不存在
            return $res;
        }

        //获取玩家自己的活动时间
        $platform = UserService::get()->getInfo($userId, GameConst::USER_INFO_TYPE_USER_PLATFORM);
        $platformPre = GameConst::PLATFORM_TIME_ARRAY[$platform];
        $startTime = strtotime($activity[$platformPre.'TriggerTime']);
        $endTime = strtotime($activity[$platformPre.'EndTime']);
        if ($christmasFloorData){
            $startTime = $christmasFloorData->getCreateTime();
            $endTime = $christmasFloorData->getEndTime();
        }
        if (!$startTime || $startTime > $reqTime) {
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['START_ERR'];//开始时间错误
            return $res;
        }
        if (!$endTime || $endTime <= $reqTime) {
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['END_ERR'];//结束时间错误
            return $res;
        }
        if ($activity['type'] != self::ActivityChristmas) {
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['ACT_TYPE_ERR'];//活动类型错误
            return $res;
        }

        if ($christmasFloorData->getPhysical() - $christmasFloorData->getUsePhysical() <= 0){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['PHYSICAL_NOT_ENOUGH'];//体力不足
            return $res;
        }

        //验证层级是否已经开启
        $openState = 0;
        $leftNum = 0;
        $floorCellsNum = $cellsConf['squareNum'];
        $dropStateArr = array();
        if ($floor == 1){
            if ($christmasFloorData->getNum1() >= $floorCellsNum - 1){
                $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['CELL_OPENED'];//不能翻
                return $res;
            }
            $openState = $christmasFloorData->getFirst();
            $leftNum = $floorCellsNum - $christmasFloorData->getNum1() - 1;
            $dropStateArr = explode(',',json_decode($christmasFloorData->getDropSpecial1()));
            $christmasFloorData->setNum1($christmasFloorData->getNum1() + 1);
        }
        if ($floor == 2){
            if ($christmasFloorData->getNum2() >= $floorCellsNum - 1){
                $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['CELL_OPENED'];//不能翻
                return $res;
            }
            $openState = $christmasFloorData->getSecond();
            $leftNum = $floorCellsNum - $christmasFloorData->getNum2() - 1;
            $dropStateArr = explode(',',json_decode($christmasFloorData->getDropSpecial2()));
            $christmasFloorData->setNum2($christmasFloorData->getNum2() + 1);
        }
        if ($floor == 3){
            if ($christmasFloorData->getNum3() >= $floorCellsNum - 1){
                $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['CELL_OPENED'];//不能翻
                return $res;
            }
            $openState = $christmasFloorData->getThird();
            $leftNum = $floorCellsNum - $christmasFloorData->getNum3() - 1;
            $dropStateArr = explode(',',json_decode($christmasFloorData->getDropSpecial3()));
            $christmasFloorData->setNum3($christmasFloorData->getNum3() + 1);
        }
        if (!$openState){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['FLOOR_NOT_OPEN'];//层级未开启
            return $res;
        }

        //验证格子是否已经开启
        /**
         * @var $christmasCellsList ChristmasCellVoList
         * @var $christmasCell ChristmasCellVo
         */
        $christmasCellsList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_ACTIVITY_CHRISTMAS_CELL);
        $christmasCell = $christmasCellsList->getElement($cell,false);
        if ($christmasCell){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['CELL_OPENED'];//格子已经开启
            return $res;
        }

        //扣除体力
        $christmasFloorData->setUsePhysical($christmasFloorData->getUsePhysical() + 1);
        $christmasFloorData->setUpdateTime($reqTime);

        //整合掉落组
        $dropArr = array();
        $needDropStep = 0;
        $i = 0;
        $mustDrop = false;
        $newSpecialState = array();
        foreach ($specialCellConf as $key=>$data){
            if ($data['atFloor'] == $floor){
                $dropArr[] = $data;
                if ($leftNum >= $data['specialTrigger'][0] && $leftNum <= $data['specialTrigger'][1]){
                    $needDropStep = $i;
                }
                if ($leftNum == $data['specialTrigger'][0]){
                    $mustDrop = true;
                }
                $newSpecialState[$i] = 0;
                $i++;
            }
            else{
                continue;
            }
        }

        //验证特殊道具是否已经掉落
        $specialFlag = false;
        if ($dropStateArr[$needDropStep] != 1){
            $specialFlag = true;
        }

        $dropItem = array();
        $type = GameConst::CHRISTMAS_CELL_NORMAL_ITEM;
        $lastAdd = -1;
        $specialItem = false;
        $tempWeigh = array();
        foreach ($cellsConf['commonProbability'] as $k=>$w){
            $tempWeigh[] = array('type' => $k, 'weight' => $w);
        }

        //按照权重进行掉落相应数据
        if ($specialFlag){//添加特殊掉落
            $lastAdd = count($tempWeigh);
            $tempWeigh[$lastAdd] = array('type' => $lastAdd, 'weight' => $dropArr[$needDropStep]['specialProbability']);
        }

        $typeConf = CommonFunctionService::get()->weightRandom($tempWeigh);
        if ($lastAdd != -1 && $typeConf['type'] == $lastAdd){
            $specialItem = true;
        }

        $itemId = 0;
        $specialState = 0;
        //掉了特殊。只掉普通
        //没掉特殊，不是最后一个，权重掉，最后一个必须掉
        if (!$specialFlag){
            $j = 0;
            foreach ($cellsConf['commonItem'] as $key1=>$data){
                if ($typeConf['type'] == $j){
                    $dropItem = array($key1=>$data);
                    $itemId = $key1;
                    break;
                }
                $j++;
            }
        }
        else{
            if ($mustDrop){
                $dropItem = $dropArr[$needDropStep]['specialItem'];
                list($itemId) = (reset($dropArr[$needDropStep]['specialItem']) ? each($dropArr[$needDropStep]['specialItem']) : each($dropArr[$needDropStep]['specialItem']));
                $dropStateArr[$needDropStep] = 1;
                $specialState = 1;
            }
            else{
                if ($specialItem){//掉落特殊道具
                    $dropItem = $dropArr[$needDropStep]['specialItem'];
                    list($itemId) = (reset($dropArr[$needDropStep]['specialItem']) ? each($dropArr[$needDropStep]['specialItem']) : each($dropArr[$needDropStep]['specialItem']));
                    $dropStateArr[$needDropStep] = 1;
                    $specialState = 1;
                }
                else{//掉落普通道具
                    $j = 0;
                    foreach ($cellsConf['commonItem'] as $key1=>$data){
                        if ($typeConf['type'] == $j){
                            $dropItem = array($key1=>$data);
                            $itemId = $key1;
                            break;
                        }
                        $j++;
                    }
                }
            }
        }

        $cellRewardNum = 0;
        $hasReward = 1;
        $content = '';
        if ($itemId == GameConst::CHRISTMAS_NEXT_FLOOR){//下一层道具特殊处理
            $type = GameConst::CHRISTMAS_CELL_NEXT_FLOOR;
            $newSpecialNum = 0;
            foreach ($specialCellConf as $key=>$data){
                if ($floor + 1 == $data['atFloor']){
                    $newSpecialNum++;
                }
            }
            $newSpecialStr = '';
            for ($i = 1; $i <= $newSpecialNum; $i++ ){
                if ($newSpecialStr == ''){
                    $newSpecialStr = '0';
                }
                else{
                    $newSpecialStr = $newSpecialStr.',0';
                }
            }

            //修改下一层状态
            if ($floor == 1){
                $christmasFloorData->setSecond(1);
                $christmasFloorData->setDropSpecial2($newSpecialStr);
            }
            if ($floor == 2){
                $christmasFloorData->setThird(1);
                $christmasFloorData->setDropSpecial3($newSpecialStr);
            }
            $content = substr(GameConst::CHRISTMAS_NEXT_FLOOR,4);
            $cellRewardNum = 1;
        }
        elseif ($itemId == GameConst::CHRISTMAS_ORDER){//订单道具特殊处理
            $type = GameConst::CHRISTMAS_CELL_ORDER;
            //获得玩家身上的所有鲜花
            $seedList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_PLANT_SEED);
            $randomFlower = array();
            foreach ($seedList->toArray() as $flower=>$flowerData){
                $randomFlower[] = $flowerData['seedId'];
            }
            //随机订单所需花
            $needTimes = rand(2,3);
            for ($h = 1; $h <= $needTimes; $h++){
                $key = array_rand($randomFlower);
                if ($content == ''){
                    $content = $randomFlower[$key];
                }
                else{
                    $content = $content.','.$randomFlower[$key];
                }
                unset($randomFlower[$key]);
            }
            $hasReward = 0;
            $cellRewardNum = 1;
        }
        else{//普通道具处理
            //获得道具
            foreach ($dropItem as $item=>$num){
                GameService::get()->handleEntity($userId, $item, $num,  true);
                $cellRewardNum = $num;
                CommonFunctionService::get()->reportResourceUseDetails(GameConst::FUNC_CHRISTMAS_FUNC,$userId,1,$item.':'.$num.'|','turnCell');//获得资源上报
                if ($content == ''){
                    $content = substr($item,4);
                }
                else{
                    $content = $content.','.substr($item,4);
                }
                if ($itemId == GameConst::CHRISTMAS_GET_SHOES){
                    $christmasFloorData->setRocksNum($christmasFloorData->getRocksNum() + 1);
                }
            }
        }

        foreach ($newSpecialState as $key=>$state){
            if ($specialState && $key >= $needDropStep){
                $newSpecialState[$key] = 1;
            }
            else{
                $newSpecialState[$key] = 0;
            }
        }

        if ($specialState){
            if ($floor == 1){
                $christmasFloorData->setDropSpecial1(implode(',',$newSpecialState));
            }
            if ($floor == 2){
                $christmasFloorData->setDropSpecial2(implode(',',$newSpecialState));
            }
            if ($floor == 3){
                $christmasFloorData->setDropSpecial3(implode(',',$newSpecialState));
            }
        }

        //设置该格子具体数据
        $christmasCell = new ChristmasCellVo(
            $userId,
            $cell,
            $type,
            json_encode($content),
            $hasReward,
            $reqTime,
            $reqTime,
            $endTime,
            true
        );

        //数据提交更新
        $christmasCellsList->addElement($christmasCell);
        $christmasCellsList->setElement($cell,$christmasCell);
        SzPersister::get()->setVoList($christmasCellsList);

        $christmasCommonList->updateElement($aid, $christmasFloorData);
        SzPersister::get()->setVoList($christmasCommonList);

        $res['cell'] = $this->formatCellData($christmasCell->toArray());
        $res['num'] = $cellRewardNum;

        //消耗体力埋点
        $userLevel = UserService::get()->getUserLevel($userId);
        StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_USER_ORBIT, array(
            'clientData' => array(
                'tid' => 'Module:newChristmas|action:turnCell|',
                'item' => 'usePhysical&1',
                'value' => 1,
                'value2' => $userLevel,
            )
        ));

        return $res;
    }

    /**
     * 开宝箱
     * @param $userId
     * @param $aid
     * @param $type
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function openTreasure($userId, $aid, $type, $reqTime)
    {
        $res = array(
            'code' => 0,
            'state'=> false
        );
        $activity = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GAME_EVENT, $aid, true);
        $chestConf = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GAME_ACTIVITY_CHRISTMAS_CHEST, $type, true);
        if (!$activity || empty($activity) || !$chestConf || empty($chestConf)){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['CONFIG_NOT_EXIST'];//配置不存在
            return $res;
        }

        //获取玩家数据
        /**
         * @var $christmasCommonList ChristmasCommonVoList
         * @var $christmasFloorData ChristmasCommonVo
         */
        $christmasCommonList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_ACTIVITY_CHRISTMAS_COMMON);
        $christmasFloorData = $christmasCommonList->getElement($aid,false);
        if (!$christmasFloorData) {
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['DATA_NOT_EXIST'];//数据不存在
            return $res;
        }

        //获取玩家自己的活动时间
        $platform = UserService::get()->getInfo($userId, GameConst::USER_INFO_TYPE_USER_PLATFORM);
        $platformPre = GameConst::PLATFORM_TIME_ARRAY[$platform];
        $startTime = strtotime($activity[$platformPre.'TriggerTime']);
        $endTime = strtotime($activity[$platformPre.'EndTime']);
        if ($christmasFloorData){
            $startTime = $christmasFloorData->getCreateTime();
            $endTime = $christmasFloorData->getEndTime();
        }
        if (!$startTime || $startTime > $reqTime) {
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['START_ERR'];//开始时间错误
            return $res;
        }
        if (!$endTime || $endTime <= $reqTime) {
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['END_ERR'];//结束时间错误
            return $res;
        }
        if ($activity['type'] != self::ActivityChristmas) {
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['ACT_TYPE_ERR'];//活动类型错误
            return $res;
        }

        //获取宝箱开启相关数据
        /**
         * 获取玩家商品数据
         * @var ModuleItemVoList $itemList
         */
        $itemList = SzPersister::get()->getVoList($userId, ModuleItemConst::ORM_NAME_ITEM);
        $openState = 0;
        $boxNum = 0;
        $keyNum = 0;
        if ($type == 1){
            $boxNum = $giftItemBoxNum = $itemList->getItemDefCount(GameConst::CHRISTMAS_BRONZE_BOX);
            $keyNum = $giftItemBoxNum = $itemList->getItemDefCount(GameConst::CHRISTMAS_BRONZE_KEY);
            $openState = $christmasFloorData->getFirstTreasure();
        }
        if ($type == 2){
            $boxNum = $giftItemBoxNum = $itemList->getItemDefCount(GameConst::CHRISTMAS_SLIVER_BOX);
            $keyNum = $giftItemBoxNum = $itemList->getItemDefCount(GameConst::CHRISTMAS_SLIVER_KEY);
            $openState = $christmasFloorData->getSecondTreasure();
        }
        if ($type == 3){
            $boxNum = $giftItemBoxNum = $itemList->getItemDefCount(GameConst::CHRISTMAS_GOLD_BOX);
            $keyNum = $giftItemBoxNum = $itemList->getItemDefCount(GameConst::CHRISTMAS_GOLD_KEY);
            $openState = $christmasFloorData->getThirdTreasure();
        }

        if ($openState == 1){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['CHEST_HAS_OPEN'];//宝箱已经开启
            return $res;
        }
        if ($boxNum < 1 || $keyNum < 1){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['NEED_ITEM_NOT_ENOUGH'];//所需道具不足
            return $res;
        }

        //道具消耗
        foreach ($chestConf['number'] as $itemId=>$num){
            GameService::get()->handleEntity($userId, $itemId, $num,  true);
            CommonFunctionService::get()->reportResourceUseDetails(GameConst::FUNC_CHRISTMAS_FUNC,$userId,0,$itemId.':'.$num.'|','openTreasure');//消耗资源上报
        }
        //获得道具
        foreach ($chestConf['reward'] as $itemId1=>$num1){
            GameService::get()->handleEntity($userId, $itemId1, $num1,  true);
            CommonFunctionService::get()->reportResourceUseDetails(GameConst::FUNC_CHRISTMAS_FUNC,$userId,1,$itemId1.':'.$num1.'|','openTreasure');//获得资源上报
        }

        //更新宝箱状态
        if ($type == 1){
            $christmasFloorData->setFirstTreasure(1);
        }
        if ($type == 2){
            $christmasFloorData->setSecondTreasure(1);
        }
        if ($type == 3){
            $christmasFloorData->setThirdTreasure(1);
        }
        $christmasFloorData->setUpdateTime($reqTime);
        $christmasCommonList->updateElement($aid, $christmasFloorData);
        SzPersister::get()->setVoList($christmasCommonList);

        $res['state'] = true;
        return $res;
    }

    /**
     * 送礼
     * @param $userId
     * @param $aid
     * @param $targetUid
     * @param $type
     * @param $giftType
     * @param $id
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function sendGift($userId, $aid, $targetUid, $type, $giftType, $id, $reqTime)
    {
        $res = array(
            'code' => 0,
            'sendData'=> array()
        );
        $activity = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GAME_EVENT, $aid, true);
        if (!$activity || empty($activity)){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['CONFIG_NOT_EXIST'];//配置不存在
            return $res;
        }

        //获取玩家数据
        /**
         * @var $christmasCommonList ChristmasCommonVoList
         * @var $christmasFloorData ChristmasCommonVo
         */
        $christmasCommonList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_ACTIVITY_CHRISTMAS_COMMON);
        $christmasFloorData = $christmasCommonList->getElement($aid,false);
        if (!$christmasFloorData) {
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['DATA_NOT_EXIST'];//数据不存在
            return $res;
        }

        //获取玩家自己的活动时间
        $platform = UserService::get()->getInfo($userId, GameConst::USER_INFO_TYPE_USER_PLATFORM);
        $platformPre = GameConst::PLATFORM_TIME_ARRAY[$platform];
        $startTime = strtotime($activity[$platformPre.'TriggerTime']);
        $endTime = strtotime($activity[$platformPre.'EndTime']);
        if ($christmasFloorData){
            $startTime = $christmasFloorData->getCreateTime();
            $endTime = $christmasFloorData->getEndTime();
        }
        if (!$startTime || $startTime > $reqTime) {
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['START_ERR'];//开始时间错误
            return $res;
        }
        if (!$endTime || $endTime <= $reqTime) {
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['END_ERR'];//结束时间错误
            return $res;
        }
        if ($activity['type'] != self::ActivityChristmas) {
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['ACT_TYPE_ERR'];//活动类型错误
            return $res;
        }

        //获取玩家当前身上的礼物盒道具
        /**
         * 获取玩家商品数据
         * @var ModuleItemVoList $itemList
         */
        $itemList = SzPersister::get()->getVoList($userId, ModuleItemConst::ORM_NAME_ITEM);
        $giftItemBoxNum = $itemList->getItemDefCount(GameConst::CHRISTMAS_GIFT_ITEM);
        if ($giftItemBoxNum < 1){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['ITEM_NOT_ENOUGH'];//道具不足
            return $res;
        }

        //获取玩家今日赠送礼物数据
        /**
         * @var $christmasSendList ChristmasSendGiftVoList
         * @var $christmasSendData ChristmasSendGiftVo
         */
        $christmasSendList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_ACTIVITY_CHRISTMAS_SEND_GIFT);
        $christmasSendData = $christmasSendList->getElement($aid,false);
        if (!$christmasSendData){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['DATA_NOT_EXIST'];//数据不存在
            return $res;
        }

        //获取玩家今日已经赠送总次数
        $totalTimes = $christmasSendData->getSendTimes();
        $dayLimit = SzConfig::get()->loadAppConfig(ModuleProfileConst::CONFIG_PROFILE, 'treasureGiftGive', true)['value'];
        if ($totalTimes >= $dayLimit){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['DATA_NOT_EXIST'];//数据不存在
            return $res;
        }

        $todaySendFriends = array();
        //获取玩家今日已经赠送的好友数据
        $sendFriendStr = json_decode($christmasSendData->getTargetUids());
        if ($sendFriendStr != ''){
            $todaySendFriends = explode(',',$sendFriendStr);
        }
        if (in_array($targetUid,$todaySendFriends)){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['FRIEND_HAS_SEND'];//该好友今日已经赠送
            return $res;
        }

        $commonRedisDb = SzDbFactory::get()->getDb(null, SzAbstractDb::DB_TYPE_REDIS);
        //获取玩家送礼配对标记
        $direction = '';
        $flag1 = $commonRedisDb->get(GameConst::CACHE_KEY_CHRISTMAS_UT_FLAG.$targetUid.'-'.$userId.'-'.$aid);
        $flag2 = $commonRedisDb->get(GameConst::CACHE_KEY_CHRISTMAS_UT_FLAG.$userId.'-'.$targetUid.'-'.$aid);
        if (!$flag1 && !$flag2){
            $direction = $targetUid.'-'.$userId;
            $commonRedisDb->set(GameConst::CACHE_KEY_CHRISTMAS_UT_FLAG.$direction.'-'.$aid,1);
        }
        else{
            //设置获取方向
            if ($flag1){
                $direction = $targetUid.'-'.$userId;
            }
            if ($flag2){
                $direction = $userId.'-'.$targetUid;
            }
        }
        if ($direction == ''){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['DATA_NOT_EXIST'];//数据不存在
            return $res;
        }

        $useItemArr = array();
        $tempId = -1;
        $friendSendData = array();
        if ($type){//回礼
            //获取所有好友赠送的礼物
            $list = $commonRedisDb->lRange(GameConst::CACHE_KEY_CHRISTMAS_GET_GIFT_LIST.$direction.'-'.$aid,0,-1);
            if (!$list){
                $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['DATA_NOT_EXIST'];//数据不存在
                return $res;
            }
            if ($list) {
                foreach ($list as $key => $val) {
                    $val = json_decode($val,true);
                    if ($val['id'] == $id && $val['getUid'] == $userId){
                        $friendSendData = $val;
                        $tempId = $key;
                    }
                }
            }

            if (empty($friendSendData) || $tempId < 0){
                $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['DATA_NOT_EXIST'];//数据不存在
                return $res;
            }
            if ($giftType){
                $giftBoxNum = $itemList->getItemDefCount(GameConst::CHRISTMAS_GIFT_BOX);
                if ($giftBoxNum < 1){
                    $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['ITEM_NOT_ENOUGH'];//道具不足
                    return $res;
                }
                $useItemArr['7401'.GameConst::CHRISTMAS_GIFT_BOX] = 1;
            }
            $useItemArr['7401'.GameConst::CHRISTMAS_GIFT_ITEM] = 1;
            if ($friendSendData['isBack']){
                $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['GIFT_BACK'];//已回礼
                return $res;
            }

            $friendSendData['isBack'] = 1;
            $friendSendData['backType'] = $giftType;
            $friendSendData['backTime'] = $reqTime;
        }
        else{//赠送
            if ($giftType){//高级
                $giftBoxNum = $itemList->getItemDefCount(GameConst::CHRISTMAS_GIFT_BOX);
                if ($giftBoxNum < 1){
                    $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['ITEM_NOT_ENOUGH'];//道具不足
                    return $res;
                }
                $useItemArr['7401'.GameConst::CHRISTMAS_GIFT_BOX] = 1;
            }
            $useItemArr['7401'.GameConst::CHRISTMAS_GIFT_ITEM] = 1;
        }

        //送礼者消耗道具
        $output = '';
        foreach ($useItemArr as $itemId=>$num){
            GameService::get()->handleEntity($userId, $itemId, $num,  true);
            $output .= $itemId.':'.$num.'|';
        }
        CommonFunctionService::get()->reportResourceUseDetails(GameConst::FUNC_CHRISTMAS_FUNC,$userId,0,$output,'sendGift1');//消耗资源上报

        $message = array();
        if ($type) {//回礼
            if (!empty($friendSendData) && $tempId >=0){
                //更新礼物状态
                $commonRedisDb->lSet(GameConst::CACHE_KEY_CHRISTMAS_GET_GIFT_LIST.$direction.'-'.$aid,$tempId,json_encode($friendSendData));
                $message = $friendSendData;
            }
        }
        else{
            //获取缓存数据
            $number = $commonRedisDb->get(GameConst::CACHE_KEY_CHRISTMAS_UID_TID.$direction.'-'.$aid);
            $saveNum = 0;
            if (!$number || $number == 0 || $number == '' || $number == null){
                $saveNum = 1;

            } else{
                $saveNum = $number + 1;
            }
            $commonRedisDb->set(GameConst::CACHE_KEY_CHRISTMAS_UID_TID.$direction.'-'.$aid,$saveNum);
            $message = $this->formatFriendSendGiftData($saveNum,$userId,$giftType,0,$reqTime,0,$targetUid,0,0,0);
            //送礼数据生成
            $commonRedisDb->lPush(GameConst::CACHE_KEY_CHRISTMAS_GET_GIFT_LIST.$direction.'-'.$aid,json_encode($message,true));
        }

        //设置好友收礼名单
        $friendRedisDb = SzDbFactory::get()->getDb($targetUid, SzAbstractDb::DB_TYPE_REDIS);
        //设置回礼数据
        $giftCell = $friendRedisDb->get(GameConst::CACHE_KEY_CHRISTMAS_GIFT_CELL.$targetUid.'-'.$aid);
        if (!$giftCell){
            $friendRedisDb->set(GameConst::CACHE_KEY_CHRISTMAS_GIFT_CELL.$targetUid.'-'.$aid,$userId);
        }
        else{
            $sendUids = explode(',',$giftCell);
            if (!in_array($userId,$sendUids)){
                $giftCell = $giftCell.','.$userId;
            }
            $friendRedisDb->set(GameConst::CACHE_KEY_CHRISTMAS_GIFT_CELL.$targetUid.'-'.$aid,$giftCell);
        }

        $christmasSendData->setSendTimes($totalTimes + 1);
        $christmasSendData->setUpdateTime($reqTime);
        $todaySendFriends[] = $targetUid;
        if (count($todaySendFriends) == 1){
            $christmasSendData->setTargetUids(json_encode((string)$targetUid));
        }
        else{
            $christmasSendData->setTargetUids(json_encode(implode(',',$todaySendFriends)));
        }
        $christmasSendList->updateElement($aid, $christmasSendData);
        SzPersister::get()->setVoList($christmasSendList);

        $res['sendData'] = $message;

        //送礼埋点
        $userLevel = UserService::get()->getUserLevel($userId);
        if ($giftType){//高级
            StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_USER_ORBIT, array(
                'clientData' => array(
                    'tid' => 'Module:newChristmas|action:sendGift|',
                    'item' => 'special&1',
                    'value' => 1,
                    'value2' => $userLevel,
                )
            ));
        }
        else{//普通
            StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_USER_ORBIT, array(
                'clientData' => array(
                    'tid' => 'Module:newChristmas|action:sendGift|',
                    'item' => 'general&1',
                    'value' => 1,
                    'value2' => $userLevel,
                )
            ));
        }

        return $res;
    }

    /**
     * 袜子换东西
     * @param $userId
     * @param $aid
     * @param $pos
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function socksExchange($userId, $aid, $pos, $reqTime)
    {
        $res = array(
            'code' => 0,
            'state'=> false
        );
        $activity = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GAME_EVENT, $aid, true);
        $socksConf = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GAME_ACTIVITY_CHRISTMAS_REWARD, $pos, true);

        if (!$activity || empty($activity) || !$socksConf || empty($socksConf)){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['CONFIG_NOT_EXIST'];//配置不存在
            return $res;
        }

        //获取玩家数据
        /**
         * @var $christmasCommonList ChristmasCommonVoList
         * @var $christmasFloorData ChristmasCommonVo
         */
        $christmasCommonList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_ACTIVITY_CHRISTMAS_COMMON);
        $christmasFloorData = $christmasCommonList->getElement($aid,false);
        if (!$christmasFloorData) {
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['DATA_NOT_EXIST'];//数据不存在
            return $res;
        }

        //获取玩家自己的活动时间
        $platform = UserService::get()->getInfo($userId, GameConst::USER_INFO_TYPE_USER_PLATFORM);
        $platformPre = GameConst::PLATFORM_TIME_ARRAY[$platform];
        $startTime = strtotime($activity[$platformPre.'TriggerTime']);
        $endTime = strtotime($activity[$platformPre.'EndTime']);
        if ($christmasFloorData){
            $startTime = $christmasFloorData->getCreateTime();
            $endTime = $christmasFloorData->getEndTime();
        }
        if (!$startTime || $startTime > $reqTime) {
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['START_ERR'];//开始时间错误
            return $res;
        }
        if (!$endTime || $endTime <= $reqTime) {
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['END_ERR'];//结束时间错误
            return $res;
        }
        if ($activity['type'] != self::ActivityChristmas) {
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['ACT_TYPE_ERR'];//活动类型错误
            return $res;
        }

        //获取玩家本次活动累计的袜子数量
        $hasSocksNum = $christmasFloorData->getRocksNum();
        if ($hasSocksNum < 0 || ($hasSocksNum && $hasSocksNum < $socksConf['number'])){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['ITEM_NOT_ENOUGH'];//道具不足
            return $res;
        }

        //获取袜子领奖状态
        $rewardStates = json_decode($christmasFloorData->getRocksReward());
        $tempReward = explode(',',$rewardStates);
        if ($tempReward[$pos - 1] == 1){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['REWARD_HAS_GET'];//奖励已经领取
            return $res;
        }

        //更改奖励领取状态
        $tempReward[$pos - 1] = 1;
        $newRewardsStates = implode(',',$tempReward);
        $christmasFloorData->setRocksReward($newRewardsStates);
        $christmasFloorData->setUpdateTime($reqTime);
        $christmasCommonList->updateElement($aid, $christmasFloorData);
        SzPersister::get()->setVoList($christmasCommonList);

        //发奖
        foreach ($socksConf['reward'] as $itemId=>$num){
            GameService::get()->handleEntity($userId, $itemId, $num,  true);
            CommonFunctionService::get()->reportResourceUseDetails(GameConst::FUNC_CHRISTMAS_FUNC,$userId,1,$itemId.':'.$num.'|','socksExchange');//获得资源上报
        }

        $res['state'] = true;
        return $res;
    }

    /**
     * 提交订单
     * @param $userId
     * @param $aid
     * @param $cell
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function submitOrder($userId, $aid, $cell, $reqTime)
    {
        $res = array(
            'code' => 0,
            'cell'=> array()
        );
        $activity = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GAME_EVENT, $aid, true);
        if (!$activity || empty($activity)){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['CONFIG_NOT_EXIST'];//配置不存在
            return $res;
        }

        //获取玩家数据
        /**
         * @var $christmasCommonList ChristmasCommonVoList
         * @var $christmasFloorData ChristmasCommonVo
         */
        $christmasCommonList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_ACTIVITY_CHRISTMAS_COMMON);
        $christmasFloorData = $christmasCommonList->getElement($aid,false);
        if (!$christmasFloorData) {
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['DATA_NOT_EXIST'];//数据不存在
            return $res;
        }

        //获取玩家自己的活动时间
        $platform = UserService::get()->getInfo($userId, GameConst::USER_INFO_TYPE_USER_PLATFORM);
        $platformPre = GameConst::PLATFORM_TIME_ARRAY[$platform];
        $startTime = strtotime($activity[$platformPre.'TriggerTime']);
        $endTime = strtotime($activity[$platformPre.'EndTime']);
        if ($christmasFloorData){
            $startTime = $christmasFloorData->getCreateTime();
            $endTime = $christmasFloorData->getEndTime();
        }
        if (!$startTime || $startTime > $reqTime) {
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['START_ERR'];//开始时间错误
            return $res;
        }
        if (!$endTime || $endTime <= $reqTime) {
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['END_ERR'];//结束时间错误
            return $res;
        }
        if ($activity['type'] != self::ActivityChristmas) {
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['ACT_TYPE_ERR'];//活动类型错误
            return $res;
        }

        //获取玩家格子数据
        /**
         * @var $christmasCellsList ChristmasCellVoList
         * @var $christmasCell ChristmasCellVo
         */
        $christmasCellsList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_ACTIVITY_CHRISTMAS_CELL);
        $christmasCell = $christmasCellsList->getElement($cell,false);
        if (!$christmasCell){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['DATA_NOT_EXIST'];//数据不存在
            return $res;
        }

        //验证是否为订单格子
        if ($christmasCell->getType() != GameConst::CHRISTMAS_CELL_ORDER){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['ERR_SUBMIT'];//错误提交
            return $res;
        }

        if ($christmasCell->getReward()){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['ORDER_OVER'];//订单已提交
            return $res;
        }

        //获取玩家所需要提交的鲜花数据
        $flowerStr = json_decode($christmasCell->getContent());
        $submitFlowerArr = explode(',',$flowerStr);
        $typeNum = 0;
        foreach ($submitFlowerArr as $key=>$flower){
            if ($flower == 0 || $flower == null || $flower == ''){
                continue;
            }
            else{
                $typeNum = $typeNum + 1;
            }
        }
        $needFlowerNum = 300/$typeNum;

        //获取玩家所拥有的鲜花数量
        /**
         * 获取玩家商品数据
         * @var ModuleItemVoList $itemList
         */
        $itemList = SzPersister::get()->getVoList($userId, ModuleItemConst::ORM_NAME_ITEM);
        foreach ($submitFlowerArr as $key=>$flowerId){
            if ($flowerId && $itemList->getItemDefCount($flowerId) < $needFlowerNum){
                $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['ITEM_NOT_ENOUGH'];//道具不足
                return $res;
            }
        }

        //消耗鲜花
        $output = '';
        foreach ($submitFlowerArr as $key=>$flowerId){
            if ($flowerId){
                $entityId = '7401'.$flowerId;
                GameService::get()->handleEntity($userId, $entityId, $needFlowerNum,  true);
                $output .= $entityId.':'.$needFlowerNum.'|';
            }
        }
        CommonFunctionService::get()->reportResourceUseDetails(GameConst::FUNC_CHRISTMAS_FUNC,$userId,0,$output,'submitOrder');//消耗资源上报

        //更新该格子数据
        $christmasCell->setReward(1);
        $christmasCell->setUpdateTime($reqTime);
        $christmasCellsList->updateElement($cell, $christmasCell);
        SzPersister::get()->setVoList($christmasCellsList);

        //获得奖励
        $reward = array();
        foreach ($submitFlowerArr as $key=>$flowerId){
            if ($flowerId){
                $orderRewardSetting = SzConfig::get()->loadAppConfig(GameConst::CONFIG_ORDER_REWARD, $flowerId, true);
                if (!$orderRewardSetting) {
                    continue;
                }
                $reward[GameConst::ENTITY_ACTION_GET_TOKEN] += ($needFlowerNum*$orderRewardSetting['gold']);
                $reward[GameConst::ENTITY_ACTION_GET_EXP] += ($needFlowerNum*$orderRewardSetting['experience']);
            }
        }

        if (!empty($reward)){
            $input = '';
            foreach ($reward as $itemId=>$num){
                GameService::get()->handleEntity($userId, $itemId, $num,  true);
                $input .= $itemId.':'.$num.'|';
            }
            CommonFunctionService::get()->reportResourceUseDetails(GameConst::FUNC_CHRISTMAS_FUNC,$userId,1,$input,'submitOrder');//获得资源上报
        }

        //增加体力
        $taskSend = SzConfig::get()->loadAppConfig(ModuleProfileConst::CONFIG_PROFILE, 'treasureTaskReward', true)['value'];
        $christmasFloorData->setPhysical($christmasFloorData->getPhysical() + $taskSend);
        $christmasCommonList->updateElement($aid, $christmasFloorData);
        SzPersister::get()->setVoList($christmasCommonList);

        $res['cell'] = $this->formatCellData($christmasCell->toArray());

        //提交订单埋点
        $userLevel = UserService::get()->getUserLevel($userId);
        StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_USER_ORBIT, array(
            'clientData' => array(
                'tid' => 'Module:newChristmas|action:submitOrder|',
                'item' => 'submitOrder&1',
                'value' => 1,
                'value2' => $userLevel,
            )
        ));
        return $res;
    }

    /**
     * 格式化牌子数据
     * @param $data
     * @return array
     */
    public function formatCellData($data)
    {
        $res = array(
            'cell' => $data['cell'],
            'type' => $data['type'],
            'content' => $data['content'],
            'reward' => $data['reward'],
            'time' => $data['createTime'],
        );

        return $res;
    }

    /**
     * 格式化商店数据
     * @param $data
     * @return array
     */
    public function formatShopData($data)
    {
        $res = array(
            'pos' => $data['pos'],
            'buyTimes' => $data['buyTimes'],
        );

        return $res;
    }

    /**
     * 格式化送礼数据
     * @param $number
     * @param $sendUid
     * @param $sendType
     * @param $isGetBack
     * @param $sendTime
     * @param $back
     * @param $getUid
     * @param $backType
     * @param $get
     * @param $backTime
     * @return array
     */
    public function formatFriendSendGiftData($number,$sendUid,$sendType,$isGetBack,$sendTime,$back,$getUid,$backType,$get,$backTime)
    {
        $res = array(
            'id' => $number,//数据编号
            'sendUid' => $sendUid,//送礼人id
            'sendType' => $sendType,//送礼类型
            'isGetBack' => $isGetBack,//领取回礼标记
            'sendTime' => $sendTime,//送礼时间
            'isBack' => $back,//是否回礼标记
            'getUid' => $getUid,//收礼人id
            'backType' => $backType,//回礼类型
            'isGet' => $get,//收礼人领奖标记
            'backTime' => $backTime,//回礼时间
        );

        return $res;
    }
}